package com.ruoyi.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.WebMallUserOrder;
import com.ruoyi.system.domain.WebMallUserOrderProduct;
import com.ruoyi.system.service.IWebMallUserOrderProductService;
import com.ruoyi.system.service.IWebMallUserOrderService;
import com.ruoyi.system.service.IWebUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商城订单Controller
 * 
 * @author wsliupan@126.com
 * @date 2021-06-17
 */
@Controller
@RequestMapping("/system/mallOrder")
public class WebMallUserOrderController extends BaseController
{
    private String prefix = "system/mallOrder";

    @Autowired
    private IWebMallUserOrderService webMallUserOrderService;
    @Autowired
    private IWebMallUserOrderProductService iWebMallUserOrderProductService;
    @Autowired
    private IWebUserService iWebUserService;

    @RequiresPermissions("system:mallOrder:view")
    @GetMapping()
    public String mallOrder()
    {
        return prefix + "/mallOrder";
    }

    /**
     * 查询商城订单列表
     */
    @RequiresPermissions("system:mallOrder:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WebMallUserOrder webMallUserOrder)
    {
        startPage();
        List<WebMallUserOrder> list = webMallUserOrderService.selectWebMallUserOrderList(webMallUserOrder);
        return getDataTable(list);
    }

    /**
     * 导出商城订单列表
     */
    @RequiresPermissions("system:mallOrder:export")
    @Log(title = "商城订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WebMallUserOrder webMallUserOrder)
    {
        List<WebMallUserOrder> list = webMallUserOrderService.selectWebMallUserOrderList(webMallUserOrder);
        ExcelUtil<WebMallUserOrder> util = new ExcelUtil<WebMallUserOrder>(WebMallUserOrder.class);
        return util.exportExcel(list, "商城订单数据");
    }

    /**
     * 新增商城订单
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存商城订单
     */
    @RequiresPermissions("system:mallOrder:add")
    @Log(title = "商城订单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WebMallUserOrder webMallUserOrder)
    {
        return toAjax(webMallUserOrderService.insertWebMallUserOrder(webMallUserOrder));
    }

    /**
     * 修改商城订单
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        WebMallUserOrder webMallUserOrder = null;
        if(id == null || id ==  -1){
            webMallUserOrder = new WebMallUserOrder();
        }else{
            webMallUserOrder = webMallUserOrderService.getById(id);
        }
        List<WebMallUserOrderProduct> listProduct = iWebMallUserOrderProductService.list(new QueryWrapper<WebMallUserOrderProduct>()
                .eq("order_id",webMallUserOrder.getId()));

        mmap.put("webMallUserOrder", webMallUserOrder);
        mmap.put("listProduct", listProduct);
        mmap.put("user",iWebUserService.getById(webMallUserOrder.getUserId()));
        return prefix + "/edit";
    }

    /**
     * 修改保存商城订单
     */
    @RequiresPermissions("system:mallOrder:edit")
    @Log(title = "商城订单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WebMallUserOrder webMallUserOrder)
    {
        return toAjax(webMallUserOrderService.updateWebMallUserOrder(webMallUserOrder));
    }

    /**
     * 删除商城订单
     */
    @RequiresPermissions("system:mallOrder:remove")
    @Log(title = "商城订单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(webMallUserOrderService.deleteWebMallUserOrderByIds(ids));
    }
}
